package capricornus.components.richtextfield
{
	import com.riaidea.text.RichTextField;
	
	import flash.display.DisplayObject;
	import flash.utils.ByteArray;
	
	public class RichTextFieldEx extends RichTextField
	{
		public function RichTextFieldEx()
		{
			super();
		}
		
		override protected function createSpriteRenderer():void
		{
			_spriteRenderer = new SpriteRenderEx(this);	
		}
		
		override public function insertSprite(newSprite:Object, index:int=-1, autoRender:Boolean=true, cache:Boolean=false):void
		{
			var dis:DisplayObject =  getNewSprite(newSprite);
			if( dis != null )
			{
				super.insertSprite( dis, index, autoRender, cache );
			}
		}
		
		private function getNewSprite( newSprite:Object ):DisplayObject
		{
			if( newSprite is String )
			{
				return new Face( String(newSprite) );
			}
			return Face(newSprite);
		}
		
		/**
		 * 导出字节，有压缩
		 * @return  
		 * 
		 */		
		public function exportBytes():ByteArray
		{
			var xml:XML = exportXML();
			var byte:ByteArray = new ByteArray();
			byte.writeUTF( xml.toString() );
			byte.compress();
			
			return byte;
		}
		
		public function importBytes( byte:ByteArray ):void
		{
			byte.uncompress();
			importXML( new XML( byte.readUTF() ) );
		}
	}
}